package cn.xmstore.controller;

import cn.xmstore.dao.UserDao;
import cn.xmstore.entity.User;
import cn.xmstore.globals.CODE;
import cn.xmstore.globals.ResponseResult;
import cn.xmstore.service.CheckCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author： Aeolia
 * @Description： TODO 验证码Controller
 * @Date： 2021/9/2 13:57
 */

@CrossOrigin(value = "http://34rr857292.zicp.vip", maxAge = 3600, originPatterns = "*", allowCredentials = "true")
@Controller
@RequestMapping("/checkCode")
public class CheckCodeController {
    @Autowired
    private CheckCodeService checkCodeService;
    @Autowired
    private UserDao userDao;

    @GetMapping("/register")
    @ResponseBody
    public ResponseResult register(String phoneNum) {
        try {
            //参数验证
            if (phoneNum == null || "".equals(phoneNum)) {
                return new ResponseResult(CODE.XMS_PARAMETER_LACK, null);
            }
            //查询数据库该手机号是否已被注册
            List<User> users = userDao.findByPhoneNum(phoneNum);
            if (!users.isEmpty()) {
                return new ResponseResult(CODE.XMS_USER_ACCOUNT_EXIST, null);
            }
            //调用验证码服务
            checkCodeService.register(phoneNum, 300);
            //响应结果
            return new ResponseResult(CODE.XMS_REQUEST_SUCCESS, null);
        } catch (Exception e) {
            return new ResponseResult(CODE.XMS_EXCEPTION_ERROR, null);
        }


    }
}
